<?php
use com\gdl\common\dataobject\common\ResultDO; 
use com\gdl\newagk\interfaces\JpushServiceIf;
use com\gdl\newagk\dataobject\jpush\JpushRequestDTO; 
require(dirname(dirname(__FILE__)) ."/php_sdk-master/include.php");
use MagicCrypt\MagicCrypt; 
date_default_timezone_set("PRC");
/**
 * 极光推送相关接口
 * 包含后台通知 富文本通知 应用内通知
 */
class api_JpushService extends api_Abstract implements JpushServiceIf{
	
	private static $appKey = "20ce09d4b0c6b858ca56f4a5";
	private static $secret = "4aff8638dcb3a61de42fde16";
    /**
     * 单条用户通知
     * 手机后台通知 需要一定手机应用权限
     */
    public function jpushUserNotice(JpushRequestDTO $jpush)
    { 
        $id = $jpush->id ? gdl_lib_BaseUtils::getStr($jpush->id) : "";
        $jpushcount = $jpush->jpushcount ? gdl_lib_BaseUtils::getStr($jpush->jpushcount) : "";
        $content = $jpush->content ? gdl_lib_BaseUtils::getStr($jpush->content) : "哈哈哈哈哈，这是一条消息";
        $pushdata = $jpush->pushdata ? gdl_lib_BaseUtils::getStr($jpush->pushdata) : "";
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = []; 
		$result->message = "这是测试消息";
        try{  
            # 极光推送类SDK
            $JpushClient = new JPush(self::$appKey, self::$secret);
            $pusher = $JpushClient->push();
            $pusher->setPlatform('all');
            //$pusher->addAllAudience();
			$arr = ['公司二'];
			$pusher->addTag($arr);
            $pusher->addSegmentId(200);
            $pusher->setNotificationAlert($content);
		
            try {
                # 开始推送
                $resultData = $pusher->send(); 
				var_dump($resultData);exit;
                if($resultData['http_code']==200 || $resultData['http_code']=="200"){
                    $result->message = "推送消息成功";
                    $result->code = 200;
                }else{
                    $result->message = "消息推送失败，请联系管理员";
                    $result->code = 200;
                }
                
            } catch (\JPush\Exceptions\JPushException $ex) {
               $result->message = $ex;
               $result->code = 400;
            }
        } catch (Exception $e) {
            $result->success = false;
            $result->code = $e->getCode();
            $result->message = $e->getMessage(); 
        }
        return $result; 
    }
	/**
	* 群消息推送接口
	*/
	public function jpushUserAll(JpushRequestDTO $jpush){
		
	}
	
	/**
	* 考试违规推送消息 指定alis[1,2,3]
	*/
	public function jpushUserPaper(JpushRequestDTO $jpush){
		$uid = $jpush->id ? gdl_lib_BaseUtils::getStr($jpush->id) : "";
        $content = $jpush->content ? gdl_lib_BaseUtils::getStr($jpush->content) : "哈哈哈哈哈，这是一条消息";
		$alias = $jpush->data ? $jpush->data : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = []; 
		$result->message = "这是测试消息";
		 
		try{  
			# 极光推送类SDK
            $JpushClient = new JPush(self::$appKey, self::$secret);
            $pusher = $JpushClient->push();
            $pusher->setPlatform('all');
			$arrays = ['a','b'];
			$pusher->addAlias($arrays);
            $pusher->addSegmentId('123');
			$keys = ['extras'=>['newid'=>55,'type'=>'考试']];
            $pusher->androidNotification('安卓消息',$keys);
			$pusher->iosNotification('ios消息',$keys);
			$pusher->addWinPhoneNotification('winphone','winphonetitle','',$keys);
		 
			 try {
                # 开始推送
                $resultData = $pusher->send();  
                if($resultData['http_code']==200 || $resultData['http_code']=="200"){
                    $result->message = "推送消息成功";
                    $result->code = 200;
                }else{
                    $result->message = "消息推送失败，请联系管理员";
                    $result->code = 200;
                }
                
            } catch (\JPush\Exceptions\JPushException $ex) {
               $result->message = $ex;
               $result->code = 400;
            }
		} catch (Exception $e) {
				$result->success = false;
				$result->code = $e->getCode();
				$result->message = $e->getMessage(); 
		}
		return $result; 	
	}
	
	/**
	* 自定义消息推送接口 demo
	*/
	/*public function jpushUserCustom(JpushRequestDTO $jpush){
		$uid = $jpush->id ? gdl_lib_BaseUtils::getStr($jpush->id) : "";
        $content = $jpush->content ? gdl_lib_BaseUtils::getStr($jpush->content) : "哈哈哈哈哈，这是一条消息";
		$alias = $jpush->data ? $jpush->data : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = []; 
		$result->message = "这是测试消息";
		
		try{ 
			$jpush = new service_jpush(); 
			$extras = ['extras'=>['msgsid'=>3333,'type'=>'成功了乐乐了']];
			$alias = ['18'];
			//$res = $jpush->jpushAll('唐川', $extras);  # 推送到所有
			//不指定定标题第一个参数即为自定义消息
			$res = $jpush->jpushOneMessage('唐川测试',$alias, $extras);  # 推送到指定用户 
			if($res){
				$result->code = 200;
				$result->message = "推送成功";
			}else{
				$result->code = 400;
				$result->message = "推送消息失败，接口异常";
			}
		}catch(Exception $e){
			$result->success = false;
			$result->code = $e->getCode();
			$result->message = $e->getMessage(); 
		}
		return $result;
	}*/
	/* mobpush测试 */
	public function jpushUserCustom(JpushRequestDTO $jpush){
		$uid = $jpush->id ? gdl_lib_BaseUtils::getStr($jpush->id) : "";
        $content = $jpush->content ? gdl_lib_BaseUtils::getStr($jpush->content) : "哈哈哈哈哈，这是一条消息";
		$alias = $jpush->data ? $jpush->data : "";
		
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = []; 
		$result->message = "这是测试消息";
		
		try{ 
			$mobPush = new service_mobpush(); 
			
			$content = "这是标题"; #标题 必填
			$target = 2; #类型 通过标签还是别名
			$userId = "1,2,3";; # 别名或标签组 多个逗号分隔
			$type = 1; #  1：通知栏 2：自定义
			$extras = [];
			$text = "";
			#自定义 通知
			$ret = $mobPush->pushAllNotice($content, $target, $userId, $type, $extras, $text);
			
			//$setAlias = "110";
			//$ret = $mobPush->setAlias($setAlias);
			
			//$tags = "12132";
			//$ret = $mobPush->setTags($tags);
			var_dump($ret);exit;
		}catch(Exception $e){
			$result->success = false;
			$result->code = $e->getCode();
			$result->message = $e->getMessage(); 
		}
		return $result;
	}
    
}
